<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>空气质量信息展示界面</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<h1 style="font-size: 50px;text-align: center">空气质量信息检测库</h1>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">按区域查询</label>
        <div class="layui-input-inline">
            <select name="districtId" id="districtSelect" lay-filter="districtId">

            </select>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">监测站名称</label>
            <div class="layui-input-inline">
                <input type="text" name="monitoringStation" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">起始时间</label>
            <div class="layui-input-inline">
                <input type="text" name="startTime" id="startTime" placeholder="yyyy-MM-dd" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" name="endTime" id="endTime" placeholder="yyyy-MM-dd" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>

<table id="demo" lay-filter="demo"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon">&#xe654;</i>
        </button>
    </div>
</script>

<script src="layui/layui.js"></script>

<script>
    layui.use(['table', 'form','laydate'], function () {
        //初始化table组件
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;
        var laydate = layui.laydate;

        // 渲染input,生成时间选择
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });

        //渲染table表格，将数据插入到表格中
        table.render({
            elem: "#demo",
            url: "/air/list",
            toolbar: '#toolbarDemo',
            page: true,
            cols: [[ //表头 field是你响应数据的data内的kay
                {field: 'id', title: '序号', sort: true}
                , {field: 'districtName', title: '区域'}
                , {field: 'monitorTime', title: '检测时间', sort: true}
                , {field: 'pm10', title: 'pm10', sort: true}
                , {field: 'pm25', title: 'pm25', sort: true}
                , {field: 'monitoringStation', title: '监测站'}
                , {fixed: 'right', width: 200, align: 'center', toolbar: '#barDemo'}]]
        });

        //手动发送ajax请求，获取到下拉列表需要的数据并且动态的插入到<select>标签体内，并且渲染
        $.ajax({
            url: "/district/list",
            data: null,
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                if (result.code == 0) {
                    var json = result.data;
                    var content = "<option value='0'>不限</option>";
                    for (var i = 0; i < json.length; i++) {
                        content += "<option value= '" + json[i].id + "'>" + json[i].name + "</option>";
                    }
                    console.log(content);
                    $("#districtSelect").html(content);
                    form.render('select');
                } else {
                    layer.msg(result.msg);
                }
            },
            error: function () {
                layer.msg("凉凉~~~");
            },
        });

        //监听submit的提交
        form.on('submit(demo1)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var parameter = data.field;
            table.reload('demo', {
                url: '/air/list',
                where: parameter
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //表格的头工具栏
        table.on('toolbar(demo)', function () {
            //如果头工具有多个按钮，记得判断一下
            layer.open({
                type: 2,
                title: '在线调试',
                content: 'add.html',
                area: ['700px', '550px'],
                end: function () {
                    layer.msg("刷新啦~");
                    table.reload('demo');
                }
            });
        });

        //工具条事件
        table.on('tool(demo)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'detail') { //查看
                //do somehing
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {

                    //向服务端发送删除指令
                    $.ajax({
                        url: "/air/delete",
                        data: data,
                        type: "GET",
                        dataType: "JSON",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.msg("刷新啦~");
                                table.reload('demo');
                                layer.close(index);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function () {
                            layer.msg("ll~");
                        }
                    });
                    return false;
                });
            } else if (layEvent === 'edit') { //编辑
                //do something
                layui.sessionData('air', {
                    key: 'demo'
                    , value: data
                });

                layer.open({
                    type: 2,
                    content: 'update.html',
                    area: ['700px', '550px'],
                    end: function () {
                        layer.msg("刷新啦~");
                        table.reload('demo');
                    }
                });
            }
        });


    });


</script>
</body>
</html>